/**
 * 属性模块数据操作文件
 */

const { Op } = require('sequelize');
const { sequelize } = require('./common');
const { attrs } = sequelize.models;

/**
 * 添加
 * @param {*} model 
 * @returns 
 */
const add = model => attrs.create(model);

/**
 * 修改
 * @param {*} model 
 * @returns 
 */
const update = model => attrs.update(model, { where: { id: model.id } });

/**
 * 删除
 * @param {*} id 
 * @returns 
 */
const del = id => attrs.destroy({ where: { id } });

/**
 * 获取所有
 * @param {*} user_id 
 * @returns 
 */
const getListByClassId = id => attrs.findAll({ where: { class_id: id } });

/**
 * 
 * @param {*} name 
 * @returns 
 */
const exists = (name, class_id, id = 0) => attrs.findOne({
    where: {
        name,
        class_id,
        [Op.not]: {
            id
        }
    }, attributes: ['id']
});


module.exports = {
    add,
    del,
    exists,
    update,
    getListByClassId,
}